/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vaccination;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.PropertyConfigurator;
import utils.DBController;
import utils.DBResultSet;
import utils.User;
import utils.Utils;

/**
 *
 * @author PacosHome
 */
public class Paziente_VisualizzaRichiami extends HttpServlet {
    private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(Paziente_VisualizzaRichiami.class); //avvio logger di log4j
    @Override
    public void init() {
        PropertyConfigurator.configure(Paziente_VisualizzaRichiami.class.getClassLoader().getResource("utils/log4j.properties"));
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        
        User user = (User) session.getAttribute("user");
        
        DBController dbController = DBController.getInstance();
        DBResultSet dbResult = dbController.getVaccinations(user.getId());
        ResultSet vaccination = dbResult.getResultSet();
        
        try {
            
            Utils.printHeader(request, out, "Visualizza Richiami");
            
            out.println("            <div class=\"container-fluid-medium\">\n" +
                            "               <div class=\"row-fluid\">\n" +
                            "                    <div class=\"span6\">\n" +
                            "                        <div class=\"well\">\n" +
                            "                            <ul class=\"nav nav-list\">\n" +
                            "                              <li class=\"nav-header\">Vaccini</li>\n" +
                            "                              <li class=\"active\"><a href=\"#\">Visualizza Richiami</a></li>\n" +
                            "                              <li class=\"nav-header\">Utente</li>\n" +
                            "                              <li><a href=\"Home\">Home</a></li>\n" +
                            "                              <li><a href=\"Logout\">Logout</a></li>\n" +
                            "                            </ul>\n" +
                            "                        </div>\n" +
                            "                    </div>\n" +
                            "                    <div class=\"well span6\" style=\"padding:10px;\">\n" +
                            "                       <div>\n");
            
                if(!vaccination.next()) {
                    out.println("                                           <h4>Nessuna vaccinazione effettuata</h4>\n");
                } else {
                    out.println("                                           <h4>Ultime Vaccinazioni effettuate: </h4>\n");
//                            
                    out.println("                                           <table>\n" +
                                "                                               <tr>\n" +
                                "                                                   <td width=\"180\"><b>Data</b></td>\n" +
                                "                                                   <td width=\"135\"><b>Dottore</b></td>\n" +
                                "                                               </tr>\n");
                    do {
                    out.println("                                               <tr>\n" +
                                "                                                   <td>" + Utils.formatDate(vaccination.getDate("date")) + "</td>\n" +
                                "                                                   <td>" + vaccination.getString("name") + " " + vaccination.getString("surname") + "</td>\n" +
                                "                                               </tr>\n");                        
                    } while(vaccination.next());
                    out.println("                                           </table>\n");
                }
                
                Utils.printFooter(out);
            
        } catch(Exception e) {
            //stampa tramite log4j il messaggio dell'eccezione e restituisce la pagine di errore
            log.error(e.getMessage());            
            Utils.printHeader(request, out, "Errore!");
            Utils.printError(request, out, e.getMessage());
            Utils.printFooter(out);
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Paziente_VisualizzaRichiami.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Paziente_VisualizzaRichiami.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
